package com.ghc.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class UpdateNoteCurator {
    /**
     * 创建会话
     */
    public static void main(String[] args) throws Exception {
        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

        //使用fluent变成风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                .namespace("base")  //独立的命名空间
                .build();
        client.start();
        System.out.println("会话2建立成功");

        String path = "/ghc-curator/c1";

        // 获取节点的数据内容及状态信息

        //数据内容
        byte[] bytes = client.getData().forPath(path);
        System.out.println("获取到的节点数据内容： " + new String(bytes));

        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath(path);
        System.out.println("获取到的节点状态信息" + stat);

        //更新节点内容
        int version = client.setData().withVersion(stat.getAversion()).forPath(path, "修改内容1".getBytes()).getVersion();

        System.out.println("当前最新版本是： " + version);

        //数据内容
        byte[] bytes2 = client.getData().forPath(path);
        System.out.println("获取到的节点数据内容： " + new String(bytes2));

        // BadVersionException
        client.setData().withVersion(stat.getVersion()).forPath(path, "修改内容2".getBytes());
    }
}
